package thread.create;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class ThreadCallableTest {
    public static void main(String[] args) {
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("call() start0, time = " + System.currentTimeMillis());

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("call() in Interruption : " + e.getMessage() + ", time = " + System.currentTimeMillis());

                    System.out.println("call() start1, time = " + System.currentTimeMillis());

                    return "失败！";
                }

                System.out.println("call() start2, time = " + System.currentTimeMillis());

                return "成功";
            }
        };
        FutureTask<String> featureTask = new FutureTask<String>(callable);

        Thread thread = new Thread(featureTask, "thread, time = " + System.currentTimeMillis());
        thread.start();

        System.out.println("Before featureTask.get(), time = " + System.currentTimeMillis());

        try {
            System.out.println("featureTask.get() = " + featureTask.get() + ", time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println("After featureTask.get(), time = " + System.currentTimeMillis());

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        thread.interrupt();
    }
}
